package org.advanced.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author John.zhang
 * @version Id: NioRead, v 1.0 2020/12/15 15:31 ZhiYong Exp $
 */
public class NioRead {

    public static void main(String[] args) {
        try (FileInputStream fileInputStream = new FileInputStream("C:/data/io/io.txt")) {
            //针对本地磁盘文件进行操作
            FileChannel fileChannel = fileInputStream.getChannel();
            //读取数据，分配缓冲区大小
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
            //同上
            /*byte[] bytes = new byte[1024];
            ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);*/
            //这里如果大小超过1024就需要循环读取 while(fileChannel.read(byteBuffer) != -1)
            int rs = fileChannel.read(byteBuffer);
            System.out.println(new String(byteBuffer.array()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
